<?php

/**
 * This is the model class for table "product".
 *
 * The followings are the available columns in table 'product':
 * @property integer $id
 * @property string $name
 * @property string $price_from
 * @property string $content
 * @property integer $store_id
 * @property integer $type_id
 *
 * The followings are the available model relations:
 * @property Image[] $images
 * @property Type $type
 * @property Store $store
 */
class Product extends CActiveRecord
{
	public $name = 'Ej. Laptop HP C3243';
	public $price_from = 'US$ 500.00';
	public $visible = true;
	public $content = '<p><ul><li><strong>Marca</strong> marca</li><li><strong>Modelo</strong> modelo</li><li><strong>Procesador</strong> procesador</li><li><strong>Memoria</strong> memoria</li><li><strong>Pantalla</strong> pantalla</li><li><strong>Disco Duro</strong> disco duro</li><li><strong>Otros</strong> otros</li></ul></p>';
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Product the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'product';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name', 'required'),
			array('store_id, type_id', 'numerical', 'integerOnly'=>true),
			array('name, price_from', 'length', 'max'=>255),
			array('content', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, price_from, content, store_id, type_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'images' => array(self::HAS_MANY, 'Image', 'product_id'),
			'type' => array(self::BELONGS_TO, 'Type', 'type_id'),
			'store' => array(self::BELONGS_TO, 'Store', 'store_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Nombre',
			'price_from' => 'Precio (Desde)',
			'content' => 'Descripción',
			'store_id' => 'Store',
			'type_id' => 'Tipo',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('price_from',$this->price_from,true);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('store_id',$this->store_id);
		$criteria->compare('type_id',$this->type_id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function allProductsByStore($store_id, $key){
		$command = Yii::app()->db->createCommand('
SELECT
	t.name as tname,
	p.id as pid, p.name as pname, p.price_from,
	i.name as iname, i.url_80
FROM
	product p
	JOIN type t ON p.type_id = t.id
	JOIN media i ON p.media_id = i.id
WHERE
	p.store_id = :store_id
	AND (:key = \'todos\' OR p.name like :like)
	AND p.visible = true
ORDER BY
	t.id
		');
		$like = '%'.$key.'%';
		$command->bindParam(':store_id', $store_id);
		$command->bindParam(':key', $key);
		$command->bindParam(':like', $like);
		return $command->queryAll();
	}
	
	public function productDetail($product_id){
		$command = Yii::app()->db->createCommand('
SELECT
	p.id as pid, p.name as pname, p.price_from, p.content, 
	i.url_400, i.url_80, s.id as sid, s.name as sname
FROM
	product p
	JOIN media i ON p.media_id = i.id
	JOIN store s ON p.store_id = s.id
WHERE
	p.id = :product_id
		');
		$command->bindParam(':product_id', $product_id);
		return $command->queryAll();		
	}
}